package com.zkca.xf.presenter;

import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import com.zkca.xf.base.BaseFragmentPresenter;
import com.zkca.xf.manager.DataManager;
import com.zkca.xf.model.javabean.NewsBeab;
import com.zkca.xf.model.javabean.uploadbean.RequestInspect;
import com.zkca.xf.model.javabean.uploadbean.RequestPage;
import com.zkca.xf.view.NewsView;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by pc on 2018/8/7.
 */

public class NewsPresenter extends BaseFragmentPresenter {


    private DataManager dataManager;
    private NewsView newsView;

    public NewsPresenter(LifecycleProvider<FragmentEvent> provider, NewsView newsView) {
        super(provider);
        this.newsView = newsView;
        dataManager = DataManager.getInstance();
    }

    //获取新闻信息（id,page,pageSize）
    public void News(RequestPage map) {
        dataManager.news(map)
                .subscribeOn(Schedulers.io())// 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread())// UI线程处理返回接口
                .compose(getProvider().<String>bindUntilEvent(FragmentEvent.DESTROY))// onDestroy取消订阅
                .subscribe(new DefaultObserver<String>() {// 订阅
                    @Override
                    public void onNext(String newsBeab) {
                        newsView.getNews(newsBeab);
                    }

                    @Override
                    public void onError(Throwable e) {
                        newsView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        newsView.hideProgressDialog();
                    }
                });
    }
}
